package com.letao.server.controller;

import com.letao.server.constants.RSAKeyConstants;
import com.letao.server.dto.Result;
import com.letao.server.entity.BaseCategory;
import com.letao.server.entity.BaseRegion;
import com.letao.server.service.IBaseCategoryService;
import com.letao.server.service.IBaseRegionService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 查询内置表的控制类
 */
@Slf4j
@RestController
@RequestMapping("/base")
public class BaseController {
    @Resource
    IBaseCategoryService categoryService;

    @Resource
    IBaseRegionService regionService;

    /**
     * 查询内置分类表
     * 接口等级:访客
     *
     * @param id 父Id
     * @return 返回的结果
     */
    @GetMapping("/category")
    private Result getCategory(@RequestParam(required = false) Integer id) {
        List<BaseCategory> list;
        if (id == null || id == 0)
            list = categoryService.query().eq("parent_id", 0).list();
        else
            list = categoryService.query().eq("parent_id", id).list();
        return Result.success(list);
    }

    /**
     * 查询内置地区表
     * 接口等级:访客
     *
     * @param id 父Id
     * @return 返回的结果
     */
    @GetMapping("/region")
    private Result getRegion(@RequestParam(required = false) Integer id) {
        List<BaseRegion> list;
        if (id == null || id == 100000)
            list = regionService.query().eq("parent_id", 100000).list();
        else
            list = regionService.query().eq("parent_id", id).list();
        return Result.success(list);
    }

    /**
     * 查询特定种类
     * 接口等级: 访客
     *
     * @param id 种类Id
     * @return 返回的结果
     */
    @GetMapping("/category/{id}")
    private Result queryCategory(@PathVariable("id") Integer id) {
        BaseCategory category = categoryService.getById(id);
        if (category == null)
            return Result.fail("数据不存在");
        else
            return Result.success(category);
    }

    /**
     * 查询特定地点
     * 接口等级:访客
     *
     * @param id 地点Id
     * @return 返回的结果
     */
    @GetMapping("/region/{id}")
    private Result queryRegion(@PathVariable("id") Integer id) {
        BaseRegion region = regionService.getById(id);
        if (region == null)
            return Result.fail("数据不存在");
        else
            return Result.success(region);
    }

    /**
     * 获取RSA密钥用以加密密码
     * 接口等级: 访客
     *
     * @return Result 返回结果
     */
    @GetMapping("/rsa")
    private Result getRSAPrivateKey(HttpServletRequest request) {
        String ip;
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getLocalAddr();
        }
        //x-forwarded-for是
        else ip = request.getHeader("x-forwarded-for");
        log.info("ip为{}的访客获取了一次RSA公钥", ip);
        // 直接返回公钥
        return Result.success(RSAKeyConstants.PUBLIC_KEY);
    }
}
